package com.tm.orderserver.controller;


import com.tm.commonutils.dto.OrderDTO;
import com.tm.commonutils.model.OrderModel;
import com.tm.commonutils.utils.R;
import com.tm.orderserver.entity.OrderEntity;
import com.tm.orderserver.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("order1")
@Slf4j
public class OrderController {


    @Autowired
    private OrderService orderService;


    @GetMapping("getOrderTest")
    public String getOrderTest(){
        log.info("这就是订单服务");
        return "这就是订单服务";
    }



    @GetMapping("/{id}")
    public R order(@PathVariable("id")  Long id){
        log.info("这就是订单order");
        OrderEntity order = orderService.findByOrderId(id);
        OrderModel  orderModel = new OrderModel();
        BeanUtils.copyProperties(order,orderModel);
        return R.success(orderModel);
    }
    @GetMapping("/list")
    public R list(){
        log.info("这就是订单list");
        List<OrderEntity> list = orderService.list();
        List<OrderModel> resList = list.stream().map(order -> {
            OrderModel orderModel = new OrderModel();
            BeanUtils.copyProperties(order, orderModel);
            return orderModel;
        }).collect(Collectors.toList());

        return R.success(resList);
    }


    @PostMapping
    public R save(@RequestBody OrderDTO orderDTO){
        log.info("这就是订单save");
         orderService.save(orderDTO);
        return R.success();
    }
    @DeleteMapping
    public R delByOrderId(@RequestParam Long id){
        log.info("这就是订单delByOrderId");
         orderService.delByOrderId(id);
        return R.success();
    }


    @PutMapping
    public R updateById(@RequestBody OrderDTO orderDTO){
        orderService.updateById(orderDTO);
        return R.success();
    }
}
